// pages/order/orderDemolition/orderDemolition.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    work_order_id: '',
    equipment_lists: '',//缴费器信息

    stoveCode: [],//返回的炉具编码
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      work_order_id: options.work_order_id
    })
    that.getData();//获取炉具、缴费器信息
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var stoveCode = wx.getStorageSync('stoveCode') || [];
    that.setData({
      stoveCode
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.removeStorageSync('stoveCode');
  },

  //跳转选择炉具编码
  chooseStoveCode() {
    var that = this;
    wx.navigateTo({
      url: '../demolitionStoveList/demolitionStoveList?work_order_id=' + that.data.work_order_id,
    })
  },
  //删除炉具编码
  delStoveCode(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var stoveCode = that.data.stoveCode;
    stoveCode.splice(index, 1);
    that.setData({
      stoveCode
    })
    wx.setStorageSync('stoveCode', stoveCode);
  },

  //获取炉具、缴费器信息
  getData(){
    var that = this;
    app.ajax({
      url: 'Engineer/Workorder/demolitionDataTestingDetails',
      data: {
        work_order_id: that.data.work_order_id,
      },
      success: function (res) {
        if (res.data.code == 1000) {
          var dataInfo = res.data.data;
          var data_test = dataInfo.energy_saving_data_test;
          var equipment_lists = dataInfo.equipment_lists;
          equipment_lists.forEach((item,index)=>{
            item.num = 0;
          })

          that.setData({
            data_test: data_test,
            equipment_lists: equipment_lists
          })
        }
      }
    })
  },
  //缴费器眼数输入
  stoveInput(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var val = e.detail.value;
    var temp = 'equipment_lists[' + index + '].num';
    that.setData({
      [temp]: val
    })
  },
  //提交数据
  submit(){
    var that = this;
    var stove_list = [];
    var stoveCode = that.data.stoveCode;
    stoveCode.forEach((item)=>{
      stove_list.push(item.array_id);
    })
    app.ajax({
      url: 'Engineer/Workorder/demolition_data_testing_operation',
      data: {
        work_order_id: that.data.work_order_id,
        data: stove_list,
        equipment_lists: that.data.equipment_lists,
      },
      success: function (res) {
        app.showToast(res.data.message);
        if (res.data.code == 1000) {
          setTimeout(() => {
            wx.navigateBack();
          }, 1000)
        }
      }
    })
  },
})
